package com.leetcode;

import java.util.Arrays;
import java.util.HashMap;
import java.util.Map;

/**
 * 350. 两个数组的交集 II
 * HashMap
 *
 * @author fy
 * @date 2022/4/6 10:50
 */
public class Solution350_1 {

    public int[] intersect(int[] nums1, int[] nums2) {
        Map<Integer, Integer> map1 = new HashMap<>(nums1.length);
        for (int i : nums1) {
            map1.put(i, map1.getOrDefault(i, 0) + 1);
        }
        int[] nums = new int[Math.max(nums1.length, nums2.length)];
        int idx = 0;
        for (int i : nums2) {
            if (map1.getOrDefault(i, 0) > 0) {
                map1.put(i, map1.getOrDefault(i, 0) - 1);
                nums[idx++] = i;
            }
        }
        return Arrays.copyOf(nums, idx);
    }

    public static void main(String[] args) {
        /**
         * 测试map的使用, map是否有默认值
         */
        Map<Integer, Integer> map = new HashMap<>();
        System.out.println(map.containsKey(100));
        System.out.println(map.get(100));
        System.out.println(map.containsKey(100));
    }

}
